package apps;

import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import jpa.entitys.synchro.staging.AGenCode;
import jpa.entitys.synchro.staging.AGenDesc;
import jpa.entitys.synchro.staging.AGenType;
import jpa.service.synchro.staging.AGenCodeServices;
import jpa.service.synchro.staging.AGenDescServices;
import jpa.service.synchro.staging.AGenTypeServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:25:19 PM 
 * CreateBy=Chatree Siangklom
 * 
 * ApplConfiguration
 * TODO
 */
public class ApplConfiguration {
	private static final Logger log = Logger.getLogger(ApplConfiguration.class);
	
	/**
	 * Date =Nov 13, 2013 : 2:25:25 PM 
	 * CreateBy=Chatree Siangklom
	 * AGenDesc
	 * TODO
	 * @param aGenTypeId
	 * @param aGenCodeId
	 * @param genDesc_value1
	 * @param genDesc_value2
	 * @return
	 * @throws Exception
	 */
	public AGenDesc GetInterfaceChannel(BigDecimal aGenTypeId, BigDecimal aGenCodeId, String genDesc_value1, String genDesc_value2) throws Exception {
		try{
			AGenDescServices AGenDescServ = new AGenDescServices(sessionFactory.CreateEmSynchro());
			java.util.List<AGenDesc> lst = AGenDescServ.findByModule(aGenTypeId, aGenCodeId, genDesc_value1, genDesc_value2);
			
			return (CommonService.getSize(lst) == 1)? lst.get(0) : null;   
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.out.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * Date =Nov 13, 2013 : 2:25:39 PM 
	 * CreateBy=Chatree Siangklom
	 * AGenDesc
	 * TODO
	 * @return
	 * @throws Exception
	 */
	public AGenDesc GetPromisUrlHost() throws Exception {
		try{
			EntityManager emSynchro = sessionFactory.CreateEmSynchro();
			
			AGenTypeServices AGenTypeServ = new AGenTypeServices(emSynchro);
			AGenType aGenType = AGenTypeServ.findByCode("PROMIS_HOST");
			if(aGenType != null)
			{
				AGenCodeServices AGenCodeServ = new AGenCodeServices(emSynchro);
				AGenCode aGenCode = AGenCodeServ.findByCode(aGenType.getAGenTypeId(), "PROMIS_URL_HOST");
				if(aGenCode != null){
					AGenDescServices AGenDescServ = new AGenDescServices(emSynchro);
					List<AGenDesc> lst = AGenDescServ.findByGenCode(aGenType.getAGenTypeId(), aGenCode.getAGenCodeId());
					if(CommonService.getSize(lst) > 0){
						for(AGenDesc value : lst){
							if(CommonService.IsNullRString(value.getStatus()).equals("A"))
								return value;
						}
					}
					AGenDescServ = null;
				}
				AGenCodeServ = null;
			}
			AGenTypeServ = null;
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * Date =Nov 13, 2013 : 2:25:45 PM 
	 * CreateBy=Chatree Siangklom
	 * AGenDesc
	 * TODO
	 * @return
	 */
	public AGenDesc PoContractorStaticValueConfig() {
		try{
			EntityManager emSynchro = sessionFactory.CreateEmSynchro();
			AGenDescServices AGenDescServ = new AGenDescServices(emSynchro);			
			return AGenDescServ.findById(1040);
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();			
			log.error(e.getMessage());
		}
		return null;
	}

	/**
	public static void main(String[] args) {
		try{
			AGenDesc aGenDesc = new ApplConfiguration().GetInterfaceChannel(
															CommonService.ConvertStringToBigDecimal("1001"), 
															CommonService.ConvertStringToBigDecimal("1002"), 
															"COMMONS", 
															"PROJECT");
			
			System.out.print(aGenDesc.getValue3());
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	*/
}
